package xyz.zdsoft.homework_manage_module.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.entity.SysUser;
import xyz.zdsoft.common.entity.WorkSubmitInfo;
import xyz.zdsoft.common.service.ISysUserService;
import xyz.zdsoft.common.service.IWorkSubmitInfoService;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.homework_manage_module.Service.StudentTaskService;
import xyz.zdsoft.homework_manage_module.bo.StudentJob;
import xyz.zdsoft.homework_manage_module.mapper.StudentTaskMapper;
import xyz.zdsoft.homework_manage_module.mapper.TeacherOperationMapper;
import xyz.zdsoft.homework_manage_module.vo.RestEnclosure;
import xyz.zdsoft.homework_manage_module.vo.RestPerTask;
import xyz.zdsoft.homework_manage_module.vo.RestQueryHomework;
import xyz.zdsoft.homework_manage_module.vo.RestStudentJob;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentTaskServiceImpl implements StudentTaskService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    IWorkSubmitInfoService iWorkSubmitInfoService;
    @Autowired
    TeacherOperationMapper mapper;
    @Autowired
    StudentTaskMapper studentTaskMapper;
    /*查询作业*/
    @Override
    public Result<RestStudentJob> jobManagement(StudentJob studentJob) {
        String workStatus=null;
        String types=null;
        String typess=null;
        String sss=null;
        String studentId= JwtUtils.getMemberIdByJwtToken(request);//获取学生id
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",studentId);
        String  studentName=iSysUserService.getOne(queryWrapper).getName();
        if (studentJob.getType()==null||studentJob.getType()=="" ||studentJob.getType().equals("待提交")){
            sss="待提交";
            workStatus="0";

        }else if (studentJob.getType().equals("待批复")){
            sss="待批复";
            typess="带批复";
            workStatus="1";
        }else {
            sss="已批复";
            workStatus="1";
            types="已批复";
        }
        if (studentJob.getCurrent()<=0||studentJob.getCurrent()==null){
            studentJob.setCurrent(1);
        }
        if (studentJob.getSize()<=0||studentJob.getSize()==null){
            studentJob.setSize(10);
        }
        Page page= PageHelper.startPage(studentJob.getCurrent(),studentJob.getSize(),"id  DESC");
        List<RestStudentJob>   list=studentTaskMapper.jobManagement(studentJob,studentName,workStatus,typess,types);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setPages(page.getPages());//总页数
        pageInfo.setTotal(page.getTotal());//总条数
        for (int i=0;i<list.size();i++){
            if(list.get(i).getWorkType().equals("0")){
                list.get(i).setWorkType("平时作业");
            }else if(list.get(i).getWorkType().equals("1")){
                list.get(i).setWorkType("阶段性作业");
            }else {
                list.get(i).setWorkType("最终交互作业");
            }
            list.get(i).setWorkStatus(sss);

        }
        Map<String,Object> map = new HashMap<>();
        map.put("page",page.getPages());
        map.put("total",page.getTotal());
        map.put("info",list);
        return Result.ok().data(map);
    }
  /*作业任务*/
    @Override
    public Result<RestPerTask> StudentTask(Integer id) {
        RestPerTask restPerTask=studentTaskMapper.StudentTask(id);
        //获取作业id
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long  work_id=iWorkSubmitInfoService.getOne(queryWrapper).getWorkId();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(work_id,"0");
        if (list!=null && list.size()>0){
            restPerTask.setRestEnclosures(list);
        }
        return Result.ok().data(restPerTask);
    }

    /*查看老师批复详情*/
    @Override
    public Result StudentViewApproval(Integer id) {
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  teacherRemark=iWorkSubmitInfoService.getOne(queryWrapper).getTeacherRemark();
        BigDecimal score=  iWorkSubmitInfoService.getOne(queryWrapper).getScore();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"3");
        Map<String,Object> map = new HashMap<>();
        map.put("teacherRemark",teacherRemark);
        map.put("score",score);
        map.put("info",list);
        return Result.ok().data(map);
    }


    /*提交学生答题*/
    @Override
    public Result StudentAnswers(Integer id, String answerRemark) {
        String is_draft="0";
        String workStatus="1";
        boolean b=studentTaskMapper.StudentAnswers(id,answerRemark,is_draft,workStatus);
        if (b==false){
            return Result.error().message("请重新提交");
        }
        return Result.ok().message("提交成功");
    }

    /*保存草稿*/
    @Override
    public Result SaveDraft(Integer id, String answerRemark) {
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  workStatus=iWorkSubmitInfoService.getOne(queryWrapper).getWorkStatus();
        if (workStatus.equals("1")){
            return Result.error().message("你已提交不能保存草稿");
        }
        String is_draft="1";
        boolean b=studentTaskMapper.StudentAnswers(id,answerRemark,is_draft,workStatus);
        if (b==false){
            return Result.error().message("请重新提交");
        }
        return Result.ok().message("提交成功");
    }
    /*学生答题*/
    @Override
    public Result ViewAnswers(Integer id) {
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  answerRemark=iWorkSubmitInfoService.getOne(queryWrapper).getAnswerRemark();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"1");
        Map<String,Object> map = new HashMap<>();
        map.put("answerRemark",answerRemark);
        map.put("info",list);
        return Result.ok().data(map);
    }


}
